<template>
  <div class="chart-content">
    <p class="title">话务量 (PBX)</p>

    <div ref="chartRef" class="chart-box"></div>
  </div>
</template>

<script setup>
import * as echarts from 'echarts'
import { ref, onMounted, onBeforeUnmount } from 'vue'

const chartRef = ref(null)
let chartsObj = null
const initChart = () => {
  chartsObj = echarts.init(chartRef.value)

  const options = {
    grid: {
      top: '16%',
      left: '4%',
      right: '4%',
      bottom: '0%',
      containLabel: true,
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['1', '2', '3', '4', '5', '6']
    },
    yAxis: {
      type: 'value'
    },
    legend: {
      itemGap: 30,
      data: ['呼入量', '呼出量', '童话总量'],
      textStyle: {
        color: '#666'
      }
    },
    series: [
      {
        name: '呼入量',
        data: [0, 0, 3, 3, 5, 0],
        type: 'line',
        areaStyle: {}
      },
      {
        name: '呼出量',
        data: [2, 2, 4, 4, 2, 2],
        type: 'line',
        areaStyle: {}
      },
      {
        name: '童话总量',
        data: [3, 1, 0, 3, 2, 2],
        type: 'line',
        areaStyle: {}
      }
    ]
  }

  chartsObj.setOption(options)

  window.addEventListener('resize', resizeCharts)
}

const resizeCharts = () => {
  chartsObj.resize()
}

onMounted(() => {
  initChart()
})

onBeforeUnmount(() => {
  window.removeEventListener('resize', resizeCharts)
})
</script>

<style scoped lang="scss">
.chart-content {
  background-color: #fff;
  border-radius: 8px;
  padding: 16px;
  .title {
    font-weight: bold;
  }
}
.chart-box {
  height: 284px;
}
</style>
